package com.health.ts.service.register;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.ts.dao.register.RegisterMapper;
import com.health.ts.dao.register.RemoteMapper;
import com.health.ts.dao.register.SourceMapper;
import com.health.ts.dao.register.VRegisterListMapper;
import com.health.ts.entity.register.Register;
import com.health.ts.entity.register.Remote;
import com.health.ts.entity.register.Source;
import com.health.ts.entity.register.VRegisterList;
import com.health.ts.exception.BusinessException;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2017-07-05
 */
@Service
public class RegisterService extends ServiceImpl<RegisterMapper, Register>{
	
	@Autowired
	RegisterMapper registerMapper;
	
	@Autowired
	RemoteMapper  remoteMapper;
	
	@Autowired
	SourceMapper sourceMapper;
	
	@Autowired
	VRegisterListMapper vRegisterListMapper;
	
	
	/**
	 * 预约挂号
	 * @param register
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public VRegisterList register(Register register){
		synchronized(this) { 
			QueryWrapper<Source> wrapper = new QueryWrapper<Source>();
			if (StringUtils.isNotEmpty(register.getScheduId())){
				wrapper.eq("pbid", register.getScheduId()); 
			}
			wrapper.eq("is_used", "0"); 
			wrapper.orderByAsc("source_order");
			//查询未使用号源
			List<Source> listSource=sourceMapper.selectList(wrapper);
			if(listSource.size()>0){//随机
				Source source=listSource.get(0);
				Date time=source.getUse_time();
				Date currentTime=new Date();
				if(time.compareTo(currentTime)>0){//可以挂号
					source.setIs_used("1");
					int i=sourceMapper.updateById(source);
					int j=0;
					if(i>0){//写入对应的挂号记录表 
						register.setId(GenerateIdUtil.makeID());
						register.setHyid(source.getId());
						register.setPatient_id(register.getPatient_id());
						register.setCreate_time(new Date());
						register.setApply_docid(SessionUserUtil.getUserId());
						register.setIsReg("0");//未退号
						j=registerMapper.insert(register);
					}
					String registerId=register.getId();
					VRegisterList vRegisterList=null;
					if(i>0&&j>0){//返回,封装的视图信息
						vRegisterList=vRegisterListMapper.selectById(registerId);
					}
					return vRegisterList;
				}else{
					throw new BusinessException("号源已过期，无法挂号");
				}
			}else{
				throw new BusinessException("挂号失败，无对应的号源，请确认对应的排班信息！");
			}
		}
	}
	
	/**
	 * 远程门诊预约
	 * @param register
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void registerRemote(Register register){
		synchronized(this) { 
			QueryWrapper<Source> wrapper = new QueryWrapper<Source>();//先查询是否有号源
			if (StringUtils.isNotEmpty(register.getScheduId())){
				wrapper.eq("pbid", register.getScheduId()); 
			}
			wrapper.eq("is_used", "0"); 
			wrapper.orderByAsc("source_order");
			//查询未使用号源
			
			List<Source> listSource=sourceMapper.selectList(wrapper);//查询是否有可预约号源
			if(listSource.size()>0){//随机
				Source source=listSource.get(0);
				Date time=source.getUse_time();
				Date currentTime=new Date();
				if(time.compareTo(currentTime)>0){//可以挂号
					source.setIs_used("1");
					int i=sourceMapper.updateById(source);
					if(i>0){//写入对应得远程预约表
						Remote remote=new Remote();
						remote.setId(GenerateIdUtil.makeID());
						remote.setPid(register.getPatient_id());
						remote.setSid(source.getId());
						remote.setCreate_time(new Date());
						remote.setTarget_docid(register.getAssign_docid());
						remote.setTarget_did(register.getDepartment_id());
						remote.setTarget_hid(register.getHospital_id());
						remote.setOpr_user(SessionUserUtil.getUserId());
						remote.setOpr_time(new Date());
						remote.setIsReport("0");//未报告 
						remote.setIsEnble("0");//有效
						remoteMapper.insert(remote);
					}
					/*String registerId=register.getId();
					VRegisterList vRegisterList=null;
					if(i>0&&j>0){//返回,封装的视图信息
						vRegisterList=vRegisterListMapper.selectById(registerId);
					}*/
				}else{
					throw new BusinessException("号源已过期，无法挂号");
				}
			}else{
				throw new BusinessException("挂号失败，无对应的号源，请确认对应的排班信息！");
			}
		}
	}
	
	
	/**
	 * 取消预约挂号
	 * @param pid
	 * @param rid
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void unRegister(String pid,String rid){
		synchronized(this) { 
			QueryWrapper<Register> register = new QueryWrapper<Register>();
			register.eq("patient_id", pid);
			register.eq("apply_docid", SessionUserUtil.getUserId());
			register.eq("id", rid);
			int i=0;
			Register registerReg=registerMapper.selectOne(register);
			if(registerReg!=null){
				registerReg.setIsReg("1");
				registerMapper.updateById(registerReg);//更改预约状态
				String sourceId=registerReg.getHyid();
				Source source=sourceMapper.selectById(sourceId);
				Date time=source.getUse_time();
				Date currentTime=new Date();
				if(time.compareTo(currentTime)>0){//可以取消
					source.setIs_used("0");
					i=sourceMapper.updateById(source);//更新
					if(i>0){
						registerMapper.deleteById(register);
					}
				}else {
					throw new BusinessException("要取消的号源已过期无法取消！");
				}
			}else{
				throw new BusinessException("要取消的号源不存在或没有取消的权限，请检查！");
			}
		}
	}
	
	/**
	 * 取消远程门诊预约
	 * @param pid
	 * @param rid
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void unRemoteRegister(String pid,String rid){
		synchronized(this) { 
			int i=0;
			Remote remoteReg=remoteMapper.selectById(rid);
			if(remoteReg!=null){
				remoteReg.setIsEnble("1");//记录设置无效
				String sourceId=remoteReg.getSid();
				Source source=sourceMapper.selectById(sourceId);
				Date time=source.getUse_time();
				Date currentTime=new Date();
				if(time.compareTo(currentTime)>0){//可以取消
					source.setIs_used("0");
					i=sourceMapper.updateById(source);//更新
					if(i>0){
						remoteMapper.updateById(remoteReg);//更改远程预约表
					}
				}else {
					throw new BusinessException("要取消的号源已过期无法取消！");
				}
			}else{
				throw new BusinessException("要取消的号源不存在或没有取消的权限，请检查！");
			}
		}
	}
}
